package htmlPicProcesser;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;

public class ProcessBatchHtmlPic {

	private static Logger log = Logger.getLogger("downtv");
	
	private static final String PICSAVEPATH = "pic/";
	
	private static final String PICNUMINIPATH = "picNum.ini";
	
	private static final String PICEXTNM = ".gif";
	
	private static final int READLENGTH = 49;
	
	private static Map picFlagMap = null;
	
	private Properties pu = new Properties();
	
	static{
		picFlagMap = new HashMap();
		picFlagMap.put(new Integer(0xc83b), ":");
		picFlagMap.put(new Integer(0x0), ":");
		picFlagMap.put(new Integer(0xDDA7), "0");
		picFlagMap.put(new Integer(0x5600), "0");
		picFlagMap.put(new Integer(0x7aad), "1");
		picFlagMap.put(new Integer(0x660a), "1");
		picFlagMap.put(new Integer(0x997a), "2");
		picFlagMap.put(new Integer(0x8862), "2");
		picFlagMap.put(new Integer(0xc9cd), "3");
		picFlagMap.put(new Integer(0x875c), "3");
		picFlagMap.put(new Integer(0xc993), "4");
		picFlagMap.put(new Integer(0x7543), "4");
		picFlagMap.put(new Integer(0xa7dc), "5");
		picFlagMap.put(new Integer(0xf80a), "5");
		picFlagMap.put(new Integer(0x7aed), "6");
		picFlagMap.put(new Integer(0xa90a), "6");
		picFlagMap.put(new Integer(0xc3fd), "7");
		picFlagMap.put(new Integer(0x1b0a), "7");
		picFlagMap.put(new Integer(0xedc8), "8");
		picFlagMap.put(new Integer(0x5500), "8");
		picFlagMap.put(new Integer(0xed78), "9");
		picFlagMap.put(new Integer(0x5b00), "9");		
	}
	
	public ProcessBatchHtmlPic(){		
	}
	
	public void batchProHtmlPic() throws IOException{
		
		File htmlPath = new File(PICSAVEPATH);
		File[] htmls = htmlPath.listFiles(new HtmlPicFileFilter());
		if(htmls != null){
			for(int i = 0; i < htmls.length; i++){		
				log.info("process html pic: " + htmls[i].getPath());
				proHtmlPicFile(htmls[i]);				
			}
			outPicNumIni();			
		}else {
			log.info("no html pic file!");
		}
		htmlPath.delete();
	}
	
	public void loadPicNumIni() throws IOException{
		File file = new File(PICNUMINIPATH);
		URL url = file.toURL();
		pu.load(url.openStream());
	}
	
	private final void proHtmlPicFile(File htmlPic){
		
		String picKey = getPicKeyName(htmlPic.getName());
		DataInputStream in = null;
		try {
			in = new DataInputStream(new BufferedInputStream(new FileInputStream(htmlPic)));			
			
			int tempInt = 0;
			String picNum = null;
			int len = READLENGTH;
			byte[] buf = new byte[len];
			int readNum = in.read(buf, 0, len);
			if (readNum != -1) {
				try {
					tempInt = in.readUnsignedShort();
				} catch (EOFException e) {
					picNum = (String)picFlagMap.get(new Integer(0x0));
					pu.setProperty(picKey, picNum);
				}				
				picNum = (String)picFlagMap.get(new Integer(tempInt));
				pu.setProperty(picKey, picNum);
			}else {
				log.info("pro html pic file error:" + picKey);
			}
		} catch (FileNotFoundException e) {		
			log.error(e, e);
			e.printStackTrace();
		} catch (IOException e) {		
			log.error(e, e);
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					log.error(e, e);
					e.printStackTrace();
				}
			}
			in = null;			
			htmlPic.delete();
		}
	}
	
	private final void outPicNumIni() throws IOException{
		File outFile = new File(PICNUMINIPATH);
		OutputStream out = new BufferedOutputStream(new FileOutputStream(outFile));
		pu.store(out, null);
		out.close();
	}
	
	public void work() throws IOException{		
		loadPicNumIni();
		batchProHtmlPic();
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {		
		try {
			ProcessBatchHtmlPic processBatchHtmlPic = new ProcessBatchHtmlPic();
			processBatchHtmlPic.work();
		} catch (Exception e) {
			log.error(e, e);
			e.printStackTrace();
		}		
	}	
	
	private final String getPicKeyName(String picName){
		String res = null;
		int temp = picName.indexOf(PICEXTNM);
		if(temp != -1){
			res = picName.substring(0, temp);			
		}	
		return res;
	}
	
	class HtmlPicFileFilter implements  FilenameFilter{

		public boolean accept(File dir, String name) {
			boolean res = false;
			String picNm = getPicKeyName(name);
			if(picNm != null){
				if(pu.getProperty(picNm) == null){
					res = true;
				}
			}		
			
			return res;
		}		
	}
}
